package com.ai.platform.system.service.impl;

import com.ai.platform.common.util.AiPlatformUtil;
import com.ai.platform.core.exception.BusinessException;
import com.ai.platform.system.mapper.SysMenuMapper;
import com.ai.platform.model.dto.Input;
import com.ai.platform.model.dto.Output;
import com.ai.platform.system.entity.SysMenu;
import com.ai.platform.system.entity.SysUser;
import com.ai.platform.system.service.ISysMenuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author yeyingsheng
 * @since 2018-05-27
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public void loadMenu(Input input, Output output) throws BusinessException {
        SysUser sysUser= (SysUser) AiPlatformUtil.getSessionObj(input,SysUser.class);
        if (null != sysUser) {
            //根据用户ID查询所有菜单
            List<SysMenu> menuList = queryMenuListByUserId(sysUser.getId());
            List<Map<String, Object>> resultData = new ArrayList<>();
            navData(menuList, resultData, null, 1);
            output.setData(resultData);
        }
    }

    //菜单导航
    private void navData(List<SysMenu> menuList, List<Map<String, Object>> resultData, Integer pId, Integer level) {
        List<SysMenu> oneList = getMenuList(menuList, pId, level);
        if (!CollectionUtils.isEmpty(oneList)) {
            for (SysMenu oneMenu : oneList) {
                Map<String, Object> map=returnMap(oneMenu.getName(),oneMenu.getIcon(),oneMenu.getUrl(),oneMenu.getIsOpen() == 1 ? true : false);
                List<SysMenu> twoList = getMenuList(menuList, oneMenu.getId(), 2);
                if(!CollectionUtils.isEmpty(twoList)) {
                    //菜单仅支持二级
                    List<Map<String,Object>> children=new ArrayList<>();
                    for (SysMenu twoMenu : twoList) {
                        children.add(returnMap(twoMenu.getName(),twoMenu.getIcon(),twoMenu.getUrl(),twoMenu.getIsOpen() == 1 ? true : false));
                    }
                    map.put("children", children);
                }
                resultData.add(map);

            }
        }
    }

    /**
     * 返回格式
     * @param title
     * @param icon
     * @param href
     * @param spread
     * @return
     */
    private Map<String,Object> returnMap(String title,String icon,String href,boolean spread){
        Map<String, Object> map = new HashMap<>();
        map.put("title",title);
        map.put("icon",icon);
        map.put("href", href);
        map.put("spread", spread);
        return map;
    }

    /**
     * 根据PID和级别查询菜单
     *
     * @param menuList
     * @param level
     * @return
     */
    private List<SysMenu> getMenuList(List<SysMenu> menuList, Integer pId, Integer level) {
        List<SysMenu> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(menuList)) {
            for (SysMenu sysMenu : menuList) {
                if (String.valueOf(sysMenu.getLevels()).equals(String.valueOf(level))) {
                    if (null == pId) {
                        list.add(sysMenu);
                    } else {
                        if (String.valueOf(pId).equals(String.valueOf(sysMenu.getpId()))) {
                            list.add(sysMenu);
                        }
                    }
                }

            }
        }
        return list;
    }

    /**
     * 根据用户ID查询菜单列表
     *
     * @param userId
     * @return
     */
    private List<SysMenu> queryMenuListByUserId(Integer userId) {
        try {
            return this.baseMapper.queryMenuListByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
